package com.zpark.it.interceptors;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Title: LoginProtectInterceptors
 * @Author Charles
 * @Package com.zpark.it.interceptors
 * @Date 2023/7/26 22:26
 * 进来的都是要拦截的
 * 检查session中是否有数据，userinfo 有 放行，没有跳转到登录页面
 */
@Component
public class LoginProtectInterceptors implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Object userInfo = request.getSession().getAttribute("userInfo");
        if (userInfo != null) {
            //放行

        }else {
            response.sendRedirect(request.getContextPath()+"/index.html");
            return false;
        }


        //false拦截   true放行
        return true;
    }
}
